package com.yihuan.controller;

import com.yihuan.entity.RecycleProductEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.RecycleProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/21 9:29
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class RecycleProductController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    @Autowired
    RecycleProductService recycleProductService;
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findRecycleProductByRecycleProductList")
    public Map<String, Object> findRecycleProductByRecycleProductList(@RequestBody RecycleProductEntity model, HttpSession session) {
        model.setCompID((Integer) session.getAttribute("compID"));
        List<RecycleProductEntity> findRecycleProductByRecycleProductList = recycleProductService.findRecycleProductByRecycleProductList(model);
        if (findRecycleProductByRecycleProductList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_LIMIT_FALL);
            return map;
        }
        pageNumber = countRecycleProductList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findRecycleProductByRecycleProductList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countRecycleProductList(RecycleProductEntity model) {
        count = recycleProductService.countRecycleProductList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 模糊分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findRecycleProductByRecycleProductConcatList")
    public Map<String, Object> findRecycleProductByRecycleProductConcatList(@RequestBody RecycleProductEntity model, HttpSession session) {
        model.setCompID((Integer) session.getAttribute("compID"));
        List<RecycleProductEntity> findRecycleProductByRecycleProductConcatList = recycleProductService.findRecycleProductByRecycleProductConcatList(model);
        if (findRecycleProductByRecycleProductConcatList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_LIMIT_Concat_FALL);
            return map;
        }
        pageNumber = countRecycleProductConcatList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findRecycleProductByRecycleProductConcatList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_LIMIT_Concat_SUCCESS);
        return map;
    }
    
    /**
     * 统计模糊分页查询数据
     *
     * @param model
     * @return
     */
    public int countRecycleProductConcatList(RecycleProductEntity model) {
        count = recycleProductService.countRecycleProductConcatList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 删除回收品信息状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateRecycleProductByRecProductDeleteState")
    public Map<String, Object> updateRecycleProductByRecProductDeleteState(@RequestBody RecycleProductEntity model, HttpSession session) {
        int updateRecycleProductByRecProductDeleteState = recycleProductService.updateRecycleProductByRecProductDeleteState(model);
        //判断数据是否存在
        if (updateRecycleProductByRecProductDeleteState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_DELETE_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_DELETE_SUCCESS);
        return map;
    }
    
    /**
     * 更改回收品信息状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateRecycleProductByRecProductState")
    public Map<String, Object> updateRecycleProductByRecProductState(@RequestBody RecycleProductEntity model, HttpSession session) {
        int updateUserByUserState = recycleProductService.updateRecycleProductByRecProductState(model);
        //判断数据是否存在
        if (updateUserByUserState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_STATE_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_STATE_SUCCESS);
        return map;
    }
    
    /**
     * 查询回收品信息名称 按商品编号
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findRecycleProductByRecProductID")
    public Map<String, Object> findRecycleProductByRecProductID(@RequestBody RecycleProductEntity model, HttpSession session) {
        model.setCompID((Integer) session.getAttribute("compID"));
        List<RecycleProductEntity> findRecycleProductByRecProductID = recycleProductService.findRecycleProductByRecProductID(model);
        //判断数据是否存在
        if (findRecycleProductByRecProductID.size() == 1) {
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("product_List", findRecycleProductByRecProductID);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_UPATE_ID_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_UPATE_ID_FALL);
        return map;
    }
    
    /**
     * 更改回收品信息
     *
     * @param recProductImage
     * @param recProductName_1
     * @param recProductPrice_1
     * @param recProductUnit_1
     * @param recProductDetail_1
     * @param recProductID
     * @param session
     * @return
     */
    @RequestMapping("/updateRecycleProductByRecProductData")
    public Map<String, Object> updateRecycleProductByRecProductData(@RequestParam MultipartFile recProductImage,
                                                                    String recProductName_1,
                                                                    double recProductPrice_1,
                                                                    String recProductUnit_1,
                                                                    String recProductDetail_1,
                                                                    int recProductID,
                                                                    int recStyID_1,
                                                                    HttpSession session) {
        
        RecycleProductEntity model = new RecycleProductEntity();
        if (!recProductImage.isEmpty()) {
            String fileName = System.currentTimeMillis() + recProductImage.getOriginalFilename();
            String savePath = MessageConstant.file;
            File dest = new File(savePath + File.separator + fileName);
            try {
                recProductImage.transferTo(dest);
                model.setRecProductName(recProductName_1);
                model.setRecProductDetail(recProductDetail_1);
                model.setRecProductUnit(recProductUnit_1);
                model.setRecProductPrice(recProductPrice_1);
                model.setRecProductID(recProductID);
                model.setRecProductImage(fileName);
                model.setRecStyID(recStyID_1);
                int updateUserByUserState = recycleProductService.updateRecycleProductByRecProductData(model);
                //判断数据是否存在
                if (updateUserByUserState == 0) {
                    map.put("code", MessageConstant.FALL_CODE);
                    map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_UPATE_FALL);
                    return map;
                }
                //账户登录成功
                map.put("code", MessageConstant.SUCCESS_CODE);
                map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_UPATE_SUCCESS);
                return map;
            } catch (IOException e) {
                e.printStackTrace();
                map.put("code", MessageConstant.FALL_CODE);
                map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_UPATE_FALL);
                return map;
            }
        } else if (recProductImage.isEmpty()) {
            map.put("code", MessageConstant.HALF_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_UPATE_FALL);
        }
        return map;
    }
    
    /**
     * 注册回收品类型信息
     *
     * @param recProductImage
     * @param recProductName_1
     * @param recProductPrice_1
     * @param recProductUnit_1
     * @param recProductDetail_1
     * @param recStyID_1
     * @param session
     * @return
     */
    @RequestMapping("/insertRecycleProductByRecycleProductData")
    public Map<String, Object> insertRecycleProductByRecycleProductData(@RequestParam MultipartFile recProductImage,
                                                                        String recProductName_1,
                                                                        double recProductPrice_1,
                                                                        String recProductUnit_1,
                                                                        String recProductDetail_1,
                                                                        int recStyID_1,
                                                                        HttpSession session) {
        
        RecycleProductEntity model = new RecycleProductEntity();
        if (!recProductImage.isEmpty()) {
            String fileName = System.currentTimeMillis() + recProductImage.getOriginalFilename();
            String savePath = MessageConstant.file;
            File dest = new File(savePath + File.separator + fileName);
            try {
                recProductImage.transferTo(dest);
                model.setRecProductName(recProductName_1);
                model.setRecProductDetail(recProductDetail_1);
                model.setRecProductUnit(recProductUnit_1);
                model.setRecProductPrice(recProductPrice_1);
                model.setCompID((Integer) session.getAttribute("compID"));
                model.setRecProductImage(fileName);
                model.setRecStyID(recStyID_1);
                //设置时间的格式
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //获取时间的方法
                Date d = new Date();
                //将获取的时间转换成设置的时间格式进行存储
                model.setRecProductDate(sf.format(d));
                //状态
                model.setRecProductState(1);
                //商品删除状态
                model.setRecProductDeleteState(1);
                int updateUserByUserState = recycleProductService.insertRecycleProductByRecycleProductData(model);
                //判断数据是否存在
                if (updateUserByUserState == 0) {
                    map.put("code", MessageConstant.FALL_CODE);
                    map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_INSERT_FALL);
                    return map;
                }
                //账户登录成功
                map.put("code", MessageConstant.SUCCESS_CODE);
                map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_INSERT_SUCCESS);
                return map;
            } catch (IOException e) {
                e.printStackTrace();
                map.put("code", MessageConstant.FALL_CODE);
                map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_INSERT_FALL);
                return map;
            }
        } else if (recProductImage.isEmpty()) {
            map.put("code", MessageConstant.HALF_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_INSERT_FALL);
        }
        return map;
    }
    
    /**
     * 前端分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findWebRecycleProductByRecycleProductList")
    public Map<String, Object> findWebRecycleProductByRecycleProductList(@RequestBody RecycleProductEntity model, HttpSession session) {
        List<RecycleProductEntity> findRecycleProductByRecycleProductList = recycleProductService.findWebRecycleProductByRecycleProductList(model);
        if (findRecycleProductByRecycleProductList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_LIMIT_FALL);
            return map;
        }
        pageNumber = countWebRecycleProductList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findRecycleProductByRecycleProductList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 前端统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countWebRecycleProductList(RecycleProductEntity model) {
        count = recycleProductService.countWebRecycleProductList();
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * web商品详情页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/findWebRecycleProductByRecProductID")
    public Map<String, Object> findWebRecycleProductByRecProductID(@RequestBody RecycleProductEntity model, HttpSession session) {
        List<RecycleProductEntity> findRecycleProductByRecProductID = recycleProductService.findWebRecycleProductByRecProductID(model);
        //判断数据是否存在
        if (findRecycleProductByRecProductID.size() == 1) {
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("product_List", findRecycleProductByRecProductID);
            map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_SELECT_ID_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain ", MessageConstant.RECSTYLE_PRODUCT_SELECT_ID_FALL);
        return map;
    }
    
    /**
     * 前端分页查询数据按类型查询
     *
     * @param model
     * @return
     */
    @RequestMapping("/findWebRecycleProductByRecycleProductListByRecStyle")
    public Map<String, Object> findWebRecycleProductByRecycleProductListByRecStyle(@RequestBody RecycleProductEntity model, HttpSession session) {
        List<RecycleProductEntity> findWebRecycleProductByRecycleProductListByRecStyle = recycleProductService.findWebRecycleProductByRecycleProductListByRecStyle(model);
        //判断数据是否存在
        if (findWebRecycleProductByRecycleProductListByRecStyle.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_STYLE_LIMIT_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findWebRecycleProductByRecycleProductListByRecStyle);
        map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_STYLE_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 前端分页查询数据 按商品类型数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findWebRecycleProductByRecycleProductListAndRecStyle")
    public Map<String, Object> findWebRecycleProductByRecycleProductListAndRecStyle(@RequestBody RecycleProductEntity model, HttpSession session) {
        List<RecycleProductEntity> findWebRecycleProductByRecycleProductListAndRecStyle = recycleProductService.findWebRecycleProductByRecycleProductListAndRecStyle(model);
        if (findWebRecycleProductByRecycleProductListAndRecStyle.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_STYLE_ID__FALL);
            return map;
        }
        pageNumber = countWebRecycleProductListAndRecStyle(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findWebRecycleProductByRecycleProductListAndRecStyle);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_STYLE_ID_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 前端统计分页查询数据 按商品类型数据
     *
     * @param model
     * @return
     */
    public int countWebRecycleProductListAndRecStyle(RecycleProductEntity model) {
        count = recycleProductService.countWebRecycleProductListAndRecStyle(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    
    /**
     * 前端分页查询数据 按公司地址数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findWebRecycleProductByRecycleProductListAndCompanyAddress")
    public Map<String, Object> findWebRecycleProductByRecycleProductListAndCompanyAddress(@RequestBody RecycleProductEntity model, HttpSession session) {
        List<RecycleProductEntity> findWebRecycleProductByRecycleProductListAndCompanyAddress = recycleProductService.findWebRecycleProductByRecycleProductListAndCompanyAddress(model);
        if (findWebRecycleProductByRecycleProductListAndCompanyAddress.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_STYLE_COMPANY_LIMIT__FALL);
            return map;
        }
        pageNumber = countWebRecycleProductListAndCompanyAddress(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findWebRecycleProductByRecycleProductListAndCompanyAddress);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_STYLE_COMPANY_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 前端统计分页查询数据 按公司地址数据
     *
     * @param model
     * @return
     */
    public int countWebRecycleProductListAndCompanyAddress(RecycleProductEntity model) {
        count = recycleProductService.countWebRecycleProductListAndCompanyAddress(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 按月份统计数据
     *
     * @return
     */
    @RequestMapping("/findWebRecycleProductByRecycleProductCount")
    public Map<String, Object> findWebRecycleProductByRecycleProductCount(HttpSession session) {
        List<RecycleProductEntity> findWebRecycleProductByRecycleProductCount = recycleProductService.findWebRecycleProductByRecycleProductCount();
        //判断数据是否存在
        if (findWebRecycleProductByRecycleProductCount.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_MONTH_FALL);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findWebRecycleProductByRecycleProductCount);
        map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_MONTH_SUCCESS);
        return map;
    }
    
    /**
     * 前端分页查询数据 按月份数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findWebRecycleProductByRecycleProductListAndMonth")
    public Map<String, Object> findWebRecycleProductByRecycleProductListAndMonth(@RequestBody RecycleProductEntity model, HttpSession session) {
        List<RecycleProductEntity> findWebRecycleProductByRecycleProductListAndMonth = recycleProductService.findWebRecycleProductByRecycleProductListAndMonth(model);
        if (findWebRecycleProductByRecycleProductListAndMonth.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_STYLE_MONTH_LIMIT__FALL);
            return map;
        }
        pageNumber = countWebRecycleProductListAndMonth(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findWebRecycleProductByRecycleProductListAndMonth);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_STYLE_MONTH_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 前端统计分页查询数据 按月份数据
     *
     * @param model
     * @return
     */
    public int countWebRecycleProductListAndMonth(RecycleProductEntity model) {
        count = recycleProductService.countWebRecycleProductListAndMonth(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 前端分页查询数据 按公司名称数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findWebRecycleProductByRecycleProductListAndCompanyName")
    public Map<String, Object> findWebRecycleProductByRecycleProductListAndCompanyName(@RequestBody RecycleProductEntity model, HttpSession session) {
        List<RecycleProductEntity> findWebRecycleProductByRecycleProductListAndMonth = recycleProductService.findWebRecycleProductByRecycleProductListAndCompanyName(model);
        if (findWebRecycleProductByRecycleProductListAndMonth.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_COMPANY_ID_LIMIT__FALL);
            return map;
        }
        pageNumber = countWebRecycleProductListAndCompanyName(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("product_List", findWebRecycleProductByRecycleProductListAndMonth);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.RECSTYLE_WEB_PRODUCT_COMPANY_ID_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 前端统计分页查询数据 按公司名称数据
     *
     * @param model
     * @return
     */
    public int countWebRecycleProductListAndCompanyName(RecycleProductEntity model) {
        count = recycleProductService.countWebRecycleProductListAndCompanyName(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 统计数据
     *
     * @param session
     * @return
     */
    @RequestMapping("/countRecycle")
    public Map<String, Object> countUser(HttpSession session) {
        count = recycleProductService.countRecycle();
        map.put("countRecycle", count);
        return map;
    }
}
